# python-resmon - Python tools for Resmon monitoring and the Circonus API
# Copyright (c) 2010 Crown coypright
# 
# This file is part of python-resmon.
# 
# python-resmon is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# python-resmon is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with python-resmon.  If not, see <http://www.gnu.org/licenses/>.

                

class Alert(object):
    def __init__(self, severity, host, metric):
        self.severity = severity
        self.host = host
        self.metric = metric

    def __cmp__(self, ano):
        return cmp(self.severity, ano.severity)

    def __str__(self):
        return '%s: %s' % (self.host.hostname, self.alert_string())

    def alert_string(self):
        raise NotImplementedError("Subclasses must implement this method")


class ResmonErrorAlert(Alert):
    """An error about a resmon failure"""
    def __init__(self, host, message):
        super(ResmonErrorAlert, self).__init__(host.get_absence_severity(), host, None)
        self.message = message

    def alert_string(self):
        return self.message
        

class AbsentAlert(Alert):
    def __init__(self, host, metric):
        super(AbsentAlert, self).__init__(metric.get_absence_severity(), host, metric)

    def alert_string(self):
        return "%s is absent!" % self.metric.get_human_name()


class StaleAlert(Alert):
    def __init__(self, host, metric, age):
        super(StaleAlert, self).__init__(0, host, metric)
        self.age = age

    def alert_string(self):
        return "%s is stale (%ds old)!" % (self.metric.get_human_name(), self.age)


class RuleAlert(Alert):
    def __init__(self, severity, host, metric, value):
        super(RuleAlert, self).__init__(severity, host, metric)
        self.value = value

    def get_formatted_value(self):
        try:
            return self.metric.get_format() % self.value
        except TypeError:
            print "Error converting value", repr(self.value), "for metric", self.metric
            return self.value

    def alert_string(self):

        return "%s is %s!" % (self.metric.get_human_name(), self.get_formatted_value())


class Restored(Alert):
    """An alert ended. Intended as an abstract superclass."""


class MetricRestored(Restored):
    """An absent alert has been restored."""
    def __init__(self, host, metric):
        super(MetricRestored, self).__init__(metric.get_absence_severity(), host, metric)

    def alert_string(self):
        return "%s restored." % self.metric.get_human_name()


class AlertCleared(Restored):
    """A failing metric has cleared."""
    def alert_string(self):
        return "%s cleared." % self.metric.get_human_name()
